﻿#region
using System;
using System.Web.Services;
using System.Collections;
using System.Data;
using System.Web.Script.Services;
using ZhongLangDB;
#endregion


namespace ZHONGLANGWXAPP
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ScriptService]                             //令WebService成功传入Json参数，并以Json形式返回结果
    //[GenerateScriptType(typeof(Person))]        //不是必要，但推荐添加(如果Person里面再嵌套另一个复杂类型，则必要声明)


    [System.ComponentModel.ToolboxItem(false)]
    public class ZLSERVER : System.Web.Services.WebService
    {
        #region test
        [WebMethod]
        [ScriptMethod(UseHttpGet = true)]
        public string TEST(string strJsonInfo)
        {
            return "TT:" + strJsonInfo;
        }
        #endregion

        //========================

        #region 数据操作公用方法
        //SYS$PRO_SYS_USERLOGIN$SERLOGIN:13084403393$USERPASS:1 
        public static SortedList strToSorted(string strIN, ref string strProcName,ref string strSys)
        {
            string[] strTT = strIN.Split('$');
            strProcName = strTT[1]; strSys = strTT[0];
            SortedList SDList = new SortedList();
            for (int i = 2; i < strTT.Length; i++)
            { 
                string[] strParme = strTT[i].Split(':');
                string strValue = strParme[1];
                //strValue = strValue.Replace("&%^&", "$").Replace("&^%&", ":");
                strValue = System.Web.HttpUtility.UrlDecode(strValue); 
                SDList[strParme[0]] = strValue;
            }
            return SDList;
        }

        public static string DataTableJson(DataTable dt)
        { 
            string strBack = "";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string strOne = "";
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    strOne += (strOne == "" ? "" : ",") + "'" + dt.Columns[j].ColumnName + "':'" + dt.Rows[i][j].ToString() + "'";
                }
                strBack += (strBack == "" ? "" : ",") + "{" + strOne + "}";
            }
            return (strBack == "" ? "" : "[" + strBack + "]");
        }
        #endregion

        #region 获取单条或执行
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string ZHONGLANGDBQueryOne(string strJsonInfo)
        {
            try
            {
                string strProcName = "", strSys = "";
                SortedList SDList = strToSorted(strJsonInfo, ref strProcName,ref strSys); 
                string strTT = (strSys==""? DBPark.proGetDTOne(SDList, strProcName) : DBPark.proGetDTOne(strSys, SDList, strProcName));
                return strTT;
            }
            catch (Exception ex)
            {
                DBPark.txtFile.txtSaveToErr(strJsonInfo + ex.ToString());
                return "";
            }
        } 

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string ZHONGLANGDBExec(string strJsonInfo)
        {
            try
            {
                string strProcName = "", strSys = "";
                SortedList SDList = strToSorted(strJsonInfo, ref strProcName, ref strSys);

                string strTT = (strSys == "" ? DBPark.proSetDB(SDList, strProcName) : DBPark.proSetDB(strSys, SDList, strProcName) );
                return strTT;
            }
            catch (Exception ex)
            {
                DBPark.txtFile.txtSaveToErr(strJsonInfo + ex.ToString());
                return "ERR,出现异常请联系技术人员检查日志"+DateTime.Now.ToString("HH:mm:ss");
            }
        }
        #endregion

        #region 获取多条
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string ZHONGLANGDBQueryToStr(string strJsonInfo)
        {
            try
            {
                string strProcName = "", strSys = "";
                SortedList SDList = strToSorted(strJsonInfo, ref strProcName, ref strSys);
                DataTable dT = (strSys == "" ? DBPark.proGetDT(SDList, strProcName) : DBPark.proGetDT(strSys, SDList, strProcName));
                if (dT == null || dT.Rows.Count < 1)
                {
                    return "";
                }
                else
                {
                    string strTT = DataTableJson(dT);
                    return strTT;
                }
            }
            catch (Exception ex)
            {
                DBPark.txtFile.txtSaveToErr(strJsonInfo + ex.ToString());
                return null;
            }
        }
         

        [WebMethod]
        public DataTable ZHONGLANGDBQueryTable(string strJsonInfo)
        {
            try
            {
                string strProcName = "", strSys = "";
                SortedList SDList = strToSorted(strJsonInfo, ref strProcName, ref strSys);
                DataTable dT = DBPark.proGetDT(strSys, SDList, strProcName);
                return dT;
            }
            catch (Exception ex)
            {
                DBPark.txtFile.txtSaveToErr(strJsonInfo + ex.ToString());
                return null;
            }
        }
        #endregion

        //======================== 
    }
}
